package com.clean.cleantotal.bean

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import coil.load
import com.blankj.utilcode.util.AppUtils
import com.clean.cleantotal.R
import com.clean.cleantotal.databinding.ItemHomeLayoutBinding
import com.clean.cleantotal.databinding.ItemMineLayoutBinding
import com.clean.cleantotal.databinding.ItemResultLayoutBinding
import com.clean.cleantotal.databinding.ItemToolsLayoutBinding
import com.drake.brv.BindingAdapter
import com.drake.brv.item.ItemBind

enum class FunType {
    DEVICE, LARGE, BATTERY, PROCESS, EMPTY, COMPRESS, TIK, INS, TEL, WHATSAPP, CLEAN, COMPRESS_IMAGE
}

class HomeBean(
    var icon: Int, var title: String, var content: String, var funType: FunType
) : ItemBind {
    var index = 0
    var bgIndex = -2
    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        when (vh.itemViewType) {
            R.layout.item_home_layout -> {
                val item = ItemHomeLayoutBinding.bind(vh.itemView)
                item.icon.load(icon)
                item.title.text = title
                item.tvContent.text = content
                item.line.isVisible = index == 1 || index == 3
                item.line1.isVisible = index == 0 || index == 2
            }

            R.layout.item_tools_layout -> {
                val item = ItemToolsLayoutBinding.bind(vh.itemView)
                item.icon.load(icon)
                item.title.text = title
                item.content.text = content
                item.content.isVisible = content.isNotEmpty()
            }

            R.layout.item_result_layout -> {
                val item = ItemResultLayoutBinding.bind(vh.itemView)
                item.icon.load(icon)
                item.funTitle.text = title
                item.funContent.text = content
                item.funContent.isVisible = content.isNotEmpty()
                updateBgColor(bgIndex, item.resultLayout)
            }
        }
    }
}

class MineBean(
    var icon: Int, var title: String, var type: Int
) : ItemBind {
    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        val item = ItemMineLayoutBinding.bind(vh.itemView)
        item.icon.load(icon)
        item.title.text = title
        item.version.text = "V:${AppUtils.getAppVersionName()}"
        item.version.isVisible = type == 0
        item.ivBack.isVisible = type == 1
    }
}


private fun updateBgColor(index: Int, layout: ConstraintLayout) {
    when (index) {
        0 -> {
            layout.setBackgroundResource(R.drawable.w_15_top)
        }

        -1 -> {
            layout.setBackgroundResource(R.drawable.w_15_bottom)
        }

        1 -> {
            layout.setBackgroundResource(R.drawable.w_15)
        }

        else -> {
            layout.setBackgroundResource(R.drawable.w_15_center)
        }
    }
}
